use std::io::{self, Write};
use chess_ai::{Position, Side, GameResult};
use chess_ai::game::Game;
use chess_ai::moves::Move;
use chess_ai::ai::{AIPlayer, RandomAI, MinimaxAI};

fn main() {
    println!("欢迎来到中国象棋AI训练系统!");
    println!("命令列表:");
    println!("  move <from_x> <from_y> <to_x> <to_y> - 移动棋子 (例如: move 0 0 0 1)");
    println!("  undo - 撤销上一步");
    println!("  show - 显示当前棋盘");
    println!("  eval - 显示当前局面评分");
    println!("  quit - 退出游戏");
    println!();

    let mut game = Game::new();
    println!("{}", game);

    loop {
        if game.result != GameResult::Ongoing {
            println!("游戏结束! 结果: {:?}", game.result);
            break;
        }

        print!("{}方走棋 > ", if game.current_side == Side::Red { "红" } else { "黑" });
        io::stdout().flush().unwrap();

        let mut input = String::new();
        io::stdin().read_line(&mut input).unwrap();
        let input = input.trim();

        match input {
            "quit" => break,
            "show" => println!("{}", game),
            "undo" => {
                match game.undo_move() {
                    Ok(_) => println!("已撤销上一步"),
                    Err(_) => println!("无法撤销"),
                }
            }
            "eval" => {
                let score = chess_ai::utils::evaluate_position(&game);
                println!("当前局面评分: {}", score);
            }
            _ => {
                if input.starts_with("move ") {
                    let parts: Vec<&str> = input.split_whitespace().collect();
                    if parts.len() == 5 {
                        if let (Ok(from_x), Ok(from_y), Ok(to_x), Ok(to_y)) = (
                            parts[1].parse::<i8>(),
                            parts[2].parse::<i8>(),
                            parts[3].parse::<i8>(),
                            parts[4].parse::<i8>(),
                        ) {
                            let mv = Move::new(
                                Position { file: from_x, rank: from_y },
                                Position { file: to_x, rank: to_y },
                            );

                            match game.make_move(mv) {
                                Ok(_) => {
                                    println!("移动成功!");
                                    println!("{}", game);
                                    
                                    // 检查将军
                                    if game.is_in_check(game.current_side) {
                                        println!("将军!");
                                    }
                                }
                                Err(e) => println!("移动失败: {:?}", e),
                            }
                        } else {
                            println!("无效的坐标格式");
                        }
                    } else {
                        println!("移动命令格式: move <from_x> <from_y> <to_x> <to_y>");
                    }
                } else {
                    println!("未知命令");
                }
            }
        }
    }

    println!("感谢使用!");
}